package com.okcl.manager.controller;

import com.okcl.manager.service.SysUserService;
import com.okcl.model.dto.system.LoginDto;
import com.okcl.model.entity.system.SysUser;
import com.okcl.model.vo.common.Result;
import com.okcl.model.vo.common.ResultCodeEnum;
import com.okcl.model.vo.h5.UserInfoVo;
import com.okcl.model.vo.system.LoginVo;
import com.okcl.model.vo.system.SysMenuVo;
import com.okcl.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "登录接口")
@RequestMapping("/admin/system/index")
@RequiredArgsConstructor
public class IndexController {
    private final SysUserService sysUserService;

    /**
     * 用户登录接口
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Operation(summary = "获取用户信息")
    @GetMapping("/userInfo")
    public Result userInfo(@RequestHeader(name = "token") String token) {
        // UserInfoVo userInfoVo = sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.getUser();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setNickName(sysUser.getName());
        userInfoVo.setAvatar(sysUser.getAvatar());
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "用户退出")
    @GetMapping("/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户绑定菜单
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询用户绑定菜单")
    @GetMapping("/menus")
    public Result queryUserMenu() {
        List<SysMenuVo> sysMenuVos = sysUserService.queryMenuByUserId();
        return Result.build(sysMenuVos, ResultCodeEnum.SUCCESS);
    }
}
